<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	小程序粉丝管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$list=pdo_getslice('jy_h5ewm_poster',$params,array($pindex,$psize),$total,$fields);
	foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'海报记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户','type'=>'member'),
			array('name'=>'poster','title'=>'海报','type'=>'image'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'表单内容'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_h5ewm_poster',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择海报','error');
	}
	
	$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$item['avatar'] = $member['avatar'];
	$item['nickname'] = $member['nickname'];
	$item['diyform']=$item['diyform']?json_decode($item['diyform'],true):array();	
	foreach($item['diyform'] as &$v){
		if($v['type']==5 && $v['value']){
			$v['value'] = explode(";", $v['value']);
			foreach($v['value'] as $k=>&$i){
				if(empty($v['value'][$k])){
					unset($v['value'][$k]);
				}
			}
		}
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'表单信息',
		'look'=>array(
				array('type'=>'string','title'=>'用户名称','name'=>'nickname','help'=>''),
				array('type'=>'image','title'=>'用户头像','name'=>'avatar','help'=>''),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'diyform',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'value','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['starttime'] || $_RQ['endtime']){
		$_RQ['starttime']=$_RQ['starttime']?strtotime($_RQ['starttime']):0;
		$_RQ['endtime']=$_RQ['endtime']?strtotime($_RQ['endtime']):time();
		$params['createtime between']=array($_RQ['starttime'],$_RQ['endtime']);
		$return['params']['createtime']=$time_range;
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_h5ewm_poster',$params);
	if(count($order)>5000){
		exi('数量大于5000，请修改搜索条件！','error');
	}
	foreach($order as &$v){
		$member=pdo_get('core_members',array('id'=>$v['mid']),array('nickname','mobile'));
		$v['nickname']=$member['nickname'];
		$v['usermobile']=$member['mobile'];	
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['diyform']=json_decode($v['diyform'],true);
		$v['data2']='';
		if(is_array($v['diyform'])){
			foreach($v['diyform'] as $dk=>&$diyv){
				$v['data2'] .='【'.$diyv['title']."】：";
				if(is_array($diyv['value']) && $diyv['type']!=7){
					$diyv['value']=$diyv['value'][0].' '.$diyv['value'][1].' '.$diyv['value'][2].' '.$diyv['value'][3];
				}
				if($diyv['type']==7){
					$diyv['value']=$diyv['value']['province'].' '.$diyv['value']['city'].' '.$diyv['value']['district'];
				}
				$v['data2'] .=$diyv['value'];
			}
		}
	}
	$header=array(
		'nickname'=>'会员昵称',
		'usermobile'=>'会员手机',
		'createtime'=>'时间',
		'data2'=>'表单内容',
	);
	cfc('tools')->export2excel($header,$order,'打卡海报列表');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_h5ewm_poster',array('id'=>$id));
	}
	exi('删除成功！');
}
?>